
package mapper;

import Utilities.DBConnection;
import dto.DTO_ChiTietHoaDon;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MAP_ChiTietHoaDon {

    public boolean themChiTietHD(DTO_ChiTietHoaDon chitietHD)
    {
        String qry = "insert into ChiTiet_HoaDon values("+chitietHD.getMaHD() + ","+chitietHD.getMaSP()+","+chitietHD.getSoLuong()+")";
        int flag = DBConnection.executeQueryString(qry);
        if(flag == 1)
        {
            return true;
        }
        return false;

    }
    public ArrayList<DTO_ChiTietHoaDon> loadDSCTHD(int MaHD)
    {
        ArrayList<DTO_ChiTietHoaDon> dscthd = new ArrayList<DTO_ChiTietHoaDon>();
        try {
            String qry = "select MaHD,s.MaSP as MaSP,TenSP,SoLuong from ChiTiet_HoaDon c , SanPham s where c.MaSP = s.MaSP and c.MaHD = " + MaHD;
            ResultSet rs = DBConnection.getResultSet(qry);
            while (rs != null && rs.next()) {
                DTO_ChiTietHoaDon cthd = new DTO_ChiTietHoaDon();
                cthd.setMaHD(rs.getInt(1));
                cthd.setMaSP(rs.getInt(2));
                cthd.setTenSP(rs.getString(3));
                cthd.setSoLuong(rs.getInt(4));
                dscthd.add(cthd);
            }
        } catch (SQLException ex) {
            Logger.getLogger(MAP_ChiTietHoaDon.class.getName()).log(Level.SEVERE, null, ex);
        }
        return dscthd;
    }
    public boolean CapNhatCTHD(int MaHD,int SoLuong,int MaSP)
    {
        String qry = "update ChiTiet_HoaDon set SoLuong = "+SoLuong +" where MaHD = "+MaHD+" and MaSP ="+MaSP;
        int flag = DBConnection.executeQueryString(qry);
        if(flag == 1)
        {
            return true;
        }
        return false;
        
    }
    public boolean XoaCTHD(int MaHD,int MaSP)
    {
        String qry = "delete from ChiTiet_HoaDon where MaHD = "+MaHD +" and MaSP = "+MaSP;
            int flag = DBConnection.executeQueryString(qry);
        if(flag == 1)
        {
            return true;
        }
        return false;

    }
     public boolean XoaCTHD(int MaHD)
    {
        String qry = "delete from ChiTiet_HoaDon where MaHD = "+MaHD;
        int flag = DBConnection.executeQueryString(qry);
        if(flag == 1)
        {
            return true;
        }
        return false;
     }
}
